using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OgilvyOne.Model;
using OgilvyOne.BLL;

namespace OgilvyOne.ProcessFlow
{
    public class UserController
    {
        // Fields
        private string sLogonName = "";
        private string sLogonPWD = "";
        private string sUserName = "";
        private const string URL_CHECKLOGIN = "/cms/logon.aspx";
        private const string URL_DEFAULT = "/systemmanage/user/user_list.aspx";
       // private const string URL_INDEX = "index.htm";
       // private const string URL_MEMBERCREATE = "user_list.aspx";
       // private const string URL_MEMBERUPDATE = "user_modify.aspx";
       // private const string USER_KEY = "USER_KEY";

        // Methods
        public bool CheckLogon()
        {
            try
            {
                if (HttpContext.Current.Session["USER_KEY"] == null)
                {
                    HttpContext.Current.Response.Redirect("/cms/logon.aspx", true);
                    return false;
                }
                UserInfo info = (UserInfo)HttpContext.Current.Session["USER_KEY"];
                HttpContext.Current.Session.Add("USER_KEY", info);
                HttpContext.Current.Session.Timeout = 0x57e40;
                this.sUserName = info.User_Name;
                this.sLogonName = info.User_Logon_Code;
                this.sLogonPWD = info.User_Password;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool CheckLogon(Label lblScript)
        {
            try
            {
                if (HttpContext.Current.Session["USER_KEY"] == null)
                {
                    lblScript.Text = "<script language=javascript>window.parent.parent.location.href=\"/cms/logon.aspx\";</script>";
                    return false;
                }
                UserInfo info = (UserInfo)HttpContext.Current.Session["USER_KEY"];
                HttpContext.Current.Session.Add("USER_KEY", info);
                this.sUserName = info.User_Name;
                this.sLogonName = info.User_Logon_Code;
                this.sLogonPWD = info.User_Password;
                HttpContext.Current.Session.Timeout = 0x57e40;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool CreateUser(UserInfo newUserInfo)
        {
            try
            {
                new User().Insert(newUserInfo);
                HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteUser(int user_uid)
        {
            new User().Delete(user_uid);
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
        }

        public bool LogonOff(Label lblScript)
        {
            try
            {
                if (HttpContext.Current.Session["USER_KEY"] != null)
                {
                    HttpContext.Current.Session.Remove("USER_KEY");
                }
                lblScript.Text = "<script language=javascript>window.parent.location.href=\"/cms/logon.aspx\";</script>";
                return false;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
        }

        public bool ProcessLogin(string login_name, string password)
        {
            return this.ProcessLogin(login_name, password, true);
        }

        public bool ProcessLogin(string login_name, string password, bool isRedirect)
        {
            UserInfo info = new User().CheckLogon(login_name, password);
            if (info == null)
            {
                if (isRedirect)
                {
                    HttpContext.Current.Response.Redirect("/cms/logon.aspx", true);
                }
                return false;
            }
            HttpContext.Current.Session.Add("USER_KEY", info);
            return true;
        }

        public void RedirectLogonUrl(HttpResponse response)
        {
            string s = "<script language='javascript'>window.parent.parent.location.href = '/logon.aspx';</script>";
            response.Write(s);
            response.End();
        }

        public void UpdateUser(UserInfo updatedUserInfo)
        {
            new User().Update(updatedUserInfo);
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
        }

        // Properties
        public string LogonName
        {
            get
            {
                return this.sLogonName;
            }
            set
            {
                this.sLogonName = value;
            }
        }

        public string LogonPWD
        {
            get
            {
                return this.sLogonPWD;
            }
            set
            {
                this.sLogonPWD = value;
            }
        }

        public string UserName
        {
            get
            {
                return this.sUserName;
            }
            set
            {
                this.sUserName = value;
            }
        }

    }
}
